package com.whiteape.book.ui.fragment;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ImageView;

import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;

import com.app.hubert.guide.NewbieGuide;
import com.app.hubert.guide.listener.OnHighlightDrewListener;
import com.app.hubert.guide.model.GuidePage;
import com.app.hubert.guide.model.HighLight;
import com.app.hubert.guide.model.HighlightOptions;
import com.app.hubert.guide.model.RelativeGuide;
import com.gyf.immersionbar.ImmersionBar;
import com.umeng.analytics.MobclickAgent;
import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.ui.activity.MainActivity;
import com.whiteape.book.ui.activity.SearchActivity;
import com.whiteape.book.ui.base.BaseTabFragment;
import com.whiteape.book.utils.ShareUtil;
import com.whiteape.book.widget.SimpleComponent;
import com.whiteape.book.widget.guideview.Guide;
import com.whiteape.book.widget.guideview.GuideBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class HomepageFragment extends BaseTabFragment {
    @BindView(R.id.card_search)
    CardView cardSearch;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.iv_search)
    ImageView ivSearch;

    public void showGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(ivSearch)
                .setAlpha(150)
                .setHighTargetCorner(20)
                .setHighTargetPadding(10);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
//                showGuideView2();
            }
        });

        builder.addComponent(new SimpleComponent());
        Guide guide = builder.createGuide();
        guide.show(getActivity());
    }

    public void showGuide(){
        NewbieGuide.with(getActivity())
                .setLabel("guide1")
                .addGuidePage(GuidePage.newInstance()
                        .addHighLight(ivSearch)
                        .setLayoutRes(R.layout.layer_frends))
                .show();
//        HighlightOptions options = new HighlightOptions.Builder()
//                .setOnHighlightDrewListener(new OnHighlightDrewListener() {
//                    @Override
//                    public void onHighlightDrew(Canvas canvas, RectF rectF) {
//                        Paint paint = new Paint();
//                        paint.setColor(Color.WHITE);
//                        paint.setStyle(Paint.Style.STROKE);
//                        paint.setStrokeWidth(10);
//                        paint.setPathEffect(new DashPathEffect(new float[]{20, 20}, 0));
//                        canvas.drawCircle(rectF.centerX(), rectF.centerY(), rectF.width() / 2 + 30, paint);
//                    }
//                })
//                .build();
//        NewbieGuide.with(getActivity())
//                .setLabel("anchor")
////                .anchor(getView().findViewById(R.id.main_view))
////                .alwaysShow(true)//总是显示，调试时可以打开
//                .addGuidePage(GuidePage.newInstance()
//                        .addHighLightWithOptions(ivSearch, HighLight.Shape.CIRCLE, options)
//                        .setLayoutRes(R.layout.layer_frends))
//                .show();

    }
    @Override
    public void initImmersionBar() {
//        super.initImmersionBar();
//        ImmersionBar.with(this)
//                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
//                .statusBarDarkFont(false)
//                .statusBarColor(R.color.colorAccent)
//                .init();

    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_homepage;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivSearch.setOnClickListener(v -> {
            String tag = ((HomepageChildFragment) getCurrentFragment()).getTagType();
            int mediaType = MediaType.getMediaType(tag);

            startActivity(new Intent(getContext(), SearchActivity.class)
                    .putExtra(SearchActivity.INPUT_EXTRA_MEDIA_TYPE, mediaType));
        });
    }

    @Override
    protected List<Fragment> createTabFragments() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(HomepageChildFragment.newInstance(MediaType.getName(MediaType.MEDIA_TYPE_NOVEL)));
        fragmentList.add(HomepageChildFragment.newInstance(MediaType.getName(MediaType.MEDIA_TYPE_COMIC)));

        return fragmentList;
    }

    @Override
    protected List<String> createTabTitles() {
        return Arrays.asList(MediaType.getName(MediaType.MEDIA_TYPE_NOVEL), MediaType.getName(MediaType.MEDIA_TYPE_COMIC));
    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
//        mTlIndicator.setSelectedTabIndicatorColor(WaApp.getInstance().getResources().getColor(R.color.colorAccent));
//        mTlIndicator.setTabTextColors(ColorUtil.isColorLight(ThemeStore.primaryColor(WaApp.getInstance().getApplicationContext())) ? Color.BLACK : Color.WHITE,
//                ThemeStore.accentColor(MApplication.getInstance().getApplicationContext()));
        cardSearch.setOnClickListener(view -> {
            MobclickAgent.onEvent(getContext(), "homepage_search");
            startActivityByAnim(new Intent(getContext(), SearchActivity.class),
                    cardSearch, "sharedView", android.R.anim.fade_in, android.R.anim.fade_out);
        });
        ivShare.setOnClickListener(view -> {

            MobclickAgent.onEvent(getContext(), "homepage_share");
            ShareUtil.checkPermissionAndShareInFragment(this, (MainActivity) getActivity());
//            checkPermissionAndShare();
//            ShareUtil.umShare(getActivity(), ((MainActivity) getActivity()));
        });
//        showGuide();
//        showGuideView();
//        if (getActivity() != null && getActivity().getTheme() != null) {
//            //下面两句应该可以
//            Drawable drawable = ContextCompat.getDrawable(getActivity(),R.drawable.ic_share1);
//            ivShare.setImageDrawable(drawable);
//
//            //TODO 下面会出现问题，有的设备会试图去drawable-hdpi-v4中查找导致问题存在
////            VectorDrawableCompat vectorDrawableCompat = VectorDrawableCompat.create(getResources(),
////                    R.drawable.ic_share1, getActivity().getTheme());
////            if (vectorDrawableCompat != null) {
////                vectorDrawableCompat.setTint(getResources().getColor(R.color.share_btn_bg));
////                ivShare.setImageDrawable(vectorDrawableCompat);
////            }
//        }
    }

}

